package com.benefm.multipar.pad;

//数据格式以及解析 看 note1.0.txt 以及note1.1.txt

import android.util.Log;

import com.benefm.multipar.pad.event.AtmosphericPressure;
import com.benefm.multipar.pad.event.BloodPressureEvent;
import com.benefm.multipar.pad.event.CarbonDioxideEvent;
import com.benefm.multipar.pad.event.EventAction;
import com.benefm.multipar.pad.event.GyroscopeInfoEvent;
import com.benefm.multipar.pad.event.LatLngEvent;
import com.benefm.multipar.pad.event.MsgEvent;
import com.benefm.multipar.pad.util.BfmZipUtils;
import com.benefm.multipar.pad.util.StringUtil;
import com.clj.fastble.utils.HexUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class MqttHandleThread implements Runnable {

    public static final String TAG = MqttHandleThread.class.getSimpleName();

    ArrayList<Short> listResp = new ArrayList<>();
    ArrayList<Short> listLeadI = new ArrayList<>();
    ArrayList<Short> listLeadII = new ArrayList<>();
    ArrayList<Short> listLeadIII = new ArrayList<>();
    ArrayList<Short> listLeadAvr = new ArrayList<>();
    ArrayList<Short> listLeadAvl = new ArrayList<>();
    ArrayList<Short> listLeadAvf = new ArrayList<>();
    ArrayList<Short> listLeadV1 = new ArrayList<>();
    ArrayList<Short> listLeadV2 = new ArrayList<>();
    ArrayList<Short> listLeadV3 = new ArrayList<>();
    ArrayList<Short> listLeadV4 = new ArrayList<>();
    ArrayList<Short> listLeadV5 = new ArrayList<>();
    ArrayList<Short> listLeadV6 = new ArrayList<>();

    private final byte[] value;
    private int count = 0;
    private String userId = "", version = "1.0";
    private boolean hasRespWave = false;

    public MqttHandleThread(byte[] value) {
        this.value = value;
    }

    @Override
    public void run() {
        // 先判断前面四个字节
        if (value[0] == 0x00 && value[1] == 0x00 && value[2] == 0x00 && value[3] == 0x01) {
            int index = 18;
            //code个数
            byte[] byteNum = new byte[2];
            System.arraycopy(value, index, byteNum, 0, 2);
            int codeNum = byteNum[0] & 0xff | (byteNum[1] << 8);
            if (BaseApp.isDebug)
                Log.i(TAG, "code num ==" + codeNum);

            //这里区分是v1.0版本 还是v1.1版本
            index += 2;

            if ((value[20] & 0xff) < 0x20) { //说明是1.1的版本 因为1.0 的版本这里是时间 不是code码

            } else { //v1.0版本
                //文件创建时间
                byte[] bTime = new byte[7];
                System.arraycopy(value, index, bTime, 0, 7);
                if (BaseApp.isDebug)
                    Log.i(TAG, "文件创建时间 " + StringUtil.byte2HexString(bTime));
                index += 7;
                //userId
                byte[] bUserId = new byte[16];
                System.arraycopy(value, index, bUserId, 0, 16);
                userId = StringUtil.byte2HexString(bUserId);
                if (BaseApp.isDebug)
                    Log.i(TAG, "userId = " + userId);
                index += 16;
            }
            //循环解析code码和数据块
            for (int i = 0; i < codeNum; i++) {
                byte[] bCode = new byte[2];
                System.arraycopy(value, index, bCode, 0, 2);
                int code = bCode[0] & 0xff | (bCode[1] << 8) & 0xff00;
                index += 2;

                byte[] byteLen = new byte[4];
                System.arraycopy(value, index, byteLen, 0, 4);
                int dataLen = byteLen[0] & 0xff | (byteLen[1] << 8) & 0xff00 | (byteLen[2] << 16) & 0xff0000 | (byteLen[3] << 24) & 0xff000000;
                index += 4;

                byte[] data = new byte[dataLen];
                System.arraycopy(value, index, data, 0, dataLen);
                index += dataLen;

                Log.i(TAG, "code = " + code + ", data = " + StringUtil.byte2HexString(data));

                if (code == 2) {//心电数据属性 ECG_ATT
//                    0  I 导联 1=II 导联 2  III 导联 3  aVR 导联4  aVL 导联   19  CR 胸部呼吸波
                    int len = data[0];
                    byte[] leadDictionary = new byte[len];
                    System.arraycopy(data, 1, leadDictionary, 0, len);
                    //判断其是否有呼吸波
                    for (byte b : leadDictionary) {
                        if (b == 19) {
                            hasRespWave = true;
                            break;
                        }
                    }
                } else if (code == 5) {
                    byte[] byteTime = new byte[7];
                    System.arraycopy(data, 0, byteTime, 0, 7);
                    byte[] byteUid = new byte[16];
                    System.arraycopy(data, 7, byteUid, 0, 16);
                    userId = StringUtil.byte2HexString(byteUid);
                    if (BaseApp.isDebug)
                        Log.i(TAG, "userId = " + userId);

                    byte[] bVersion = new byte[2];
                    System.arraycopy(data, 23, bVersion, 0, 2);
                    if (BaseApp.isDebug)
                        Log.i(TAG, "version = " + HexUtil.formatHexString(bVersion));
                    if (bVersion[1] == 0) {
                        version = "1.0";
                    } else if (bVersion[1] == 1) {
                        version = "1.1";
                    }
                }

            }
            //循环解析数据块
            while (index < value.length) {
                index = parseData(index, value);
            }
        }

    }

    //解析数据块
    private int parseData(int index, byte[] value) {
        byte[] prams = new byte[16];
        System.arraycopy(value, index, prams, 0, 16);
        int dNum = prams[0] & 0xff | ((prams[1] << 8) & 0xff00);
        //加密类型
        byte[] bType = new byte[2];
        System.arraycopy(prams, 6, bType, 0, 2);
        int type = bType[0] & 0xff | ((bType[1] << 8) & 0xff00);
        if (BaseApp.isDebug)
            Log.i(TAG, "dNum = " + dNum + ", encode type = " + type + "[0:no,5:yes]");
        //当前类型原始长度
        byte[] bOriginal = new byte[4];
        System.arraycopy(prams, 8, bOriginal, 0, 4);
        int originalLen = bOriginal[0] & 0xff | (bOriginal[1] << 8) & 0xff00 | (bOriginal[2] << 16) & 0xff0000 | (bOriginal[3] << 24) & 0xff000000;
        //当前类型加密后的长度
        byte[] bEncode = new byte[4];
        System.arraycopy(prams, 12, bEncode, 0, 4);
        int encodeLen = bEncode[0] & 0xff | (bEncode[1] << 8) & 0xff00 | (bEncode[2] << 16) & 0xff0000 | (bEncode[3] << 24) & 0xff000000;
        //具体数据块
        int dataLen = type == 0 ? originalLen : encodeLen;
        byte[] data = new byte[dataLen];

        index += 16;

        System.arraycopy(value, index, data, 0, dataLen);

        index += dataLen;

        handleData(prams, data);

        return index;

    }

    //处理数据块
    public void handleData(byte[] prams, byte[] data) {
        //数据编码2byte
        byte[] bCode = new byte[2];
        System.arraycopy(prams, 0, bCode, 0, 2);
        int code = bCode[0] & 0xff | ((bCode[1] << 8) & 0xff00);
        //加密类型
        byte[] bType = new byte[2];
        System.arraycopy(prams, 6, bType, 0, 2);
        int type = bType[0] & 0xff | ((bType[1] << 8) & 0xff00);

        if (code == 100) {//心电数据
            short[] sData;
            if (type == 0) {
                sData = StringUtil.byteToShort(data);
                if (BaseApp.isDebug)
                    Log.i(TAG, "userId = " + userId + ",[波形数据]");
                handWaveData(sData);
            } else {
                sData = new short[data.length];
                int index = BfmZipUtils.decompress(data, data.length, sData);
                if (BaseApp.isDebug)
                    Log.i(TAG, "index = " + index);
                if (index == 4000) {//长度为4000表示解压缩正确
                    handWaveData(sData);
                }
            }
        } else if (code == 103) {//陀螺仪数据
            GyroscopeInfoEvent event = new GyroscopeInfoEvent();
            short x = (short) (data[0] & 0xff | ((data[1] << 8) & 0xff00));
            short y = (short) (data[2] & 0xff | ((data[3] << 8) & 0xff00));
            short z = (short) (data[4] & 0xff | ((data[5] << 8) & 0xff00));
            event.x = (double) x / 10000;
            event.y = (double) y / 10000;
            event.z = (double) z / 10000;
            double a = Math.acos(Math.abs(event.y) / Math.sqrt(event.x * event.x + event.y * event.y + event.z * event.z));
            event.degrees = 180 / Math.PI * a;
            Log.i(TAG, event.toString());
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_GYROSCOPE, event, userId));
        } else if (code == 104) {//血氧数据
            int spo2 = data[4] & 0xff | (data[5] << 8) & 0xff00;
            Log.i(TAG, "spo2 == " + spo2);
            if (spo2 == 0xffff) {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_SPO2_INVALID, spo2, userId));
            } else {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_SPO2, spo2, userId));
            }

        } else if (code == 105) {//血压数据
            BloodPressureEvent bpEvent = new BloodPressureEvent();
            bpEvent.sbp = data[4] & 0xff | (data[5] << 8) & 0xff00;
            bpEvent.dbp = data[6] & 0xff | (data[7] << 8) & 0xff00;
            bpEvent.pr = data[10] & 0xff | (data[11] << 8) & 0xff00;
            Log.i(TAG, bpEvent.toString());
            if (bpEvent.sbp == 0xffff) {

            } else {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BLOOD_PRESSURE, bpEvent, userId));
            }
        } else if (code == 106) {//温度
            int temperature = data[5] & 0xff | (data[6] << 8) & 0xff00;
            if (BaseApp.isDebug)
                Log.i(TAG, "temperature = " + temperature);
            if (temperature == 0x8000) {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE_INVALID, temperature, userId));
            } else {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE, temperature, userId));
            }
        } else if (code == 108) {//心电参数
            byte[] bLen = new byte[4];
            System.arraycopy(prams, 8, bLen, 0, 4);
            int len = bLen[0] & 0xff | (bLen[1] << 8) & 0xff00 | (bLen[2] << 16) & 0xff0000 | (bLen[3] << 24) & 0xff000000;
            if (BaseApp.isDebug)
                Log.i(TAG, "len == " + len);
            //心率数据====================================================
            byte[] pData = new byte[len - 8];//去除版本号相关的信息之后的数据
            System.arraycopy(data, 8, pData, 0, len - 8);
            int hr = pData[0] & 0xff | ((pData[1] << 8) & 0xff00);
            if (BaseApp.isDebug)
                Log.i(TAG, "hr = " + hr);
            if (hr != 0xffff && hr != 0xff9c)
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_ECG_HR, hr, userId));
            //报警类型
            int alarmType = pData[2] & 0xff | ((pData[3] << 8) & 0xff00);

            //I导联ST段偏移值 为实际数值的1000倍
            int stI = pData[4] & 0xff | ((pData[5] << 8) & 0xff00);
            int stII = pData[6] & 0xff | ((pData[7] << 8) & 0xff00);
            int stIII = pData[8] & 0xff | ((pData[9] << 8) & 0xff00);
            int stAvr = pData[10] & 0xff | ((pData[11] << 8) & 0xff00);
            int stAvl = pData[12] & 0xff | ((pData[13] << 8) & 0xff00);
            int stAvf = pData[14] & 0xff | ((pData[15] << 8) & 0xff00);
            int stV1 = pData[16] & 0xff | ((pData[17] << 8) & 0xff00);
            int stV2 = pData[18] & 0xff | ((pData[19] << 8) & 0xff00);
            int stV3 = pData[20] & 0xff | ((pData[21] << 8) & 0xff00);
            int stV4 = pData[22] & 0xff | ((pData[23] << 8) & 0xff00);
            int stV5 = pData[24] & 0xff | ((pData[25] << 8) & 0xff00);
            int stV6 = pData[26] & 0xff | ((pData[27] << 8) & 0xff00);
            if (BaseApp.isDebug)
                Log.i(TAG, "stI to stV6 values = " + stI + "," + stII + "," + stIII + "," + stAvr + ","
                        + stAvl + "," + stAvf + "," + stV1 + "," + stV2 + "," + stV3 + "," + stV4 + "," + stV5 + "," + stV6);
            /*
             * ......
             */
            //导联信息
            int leadPadStatusLen = pData[21 * 2] & 0xff;
            byte[] leadPad = new byte[leadPadStatusLen];
            System.arraycopy(pData, 21 * 2 + 1, leadPad, 0, leadPadStatusLen);
            if (BaseApp.isDebug)
                Log.i(TAG, "导联字典 = " + StringUtil.byte2HexString(leadPad));

            byte[] leadPadStatus = new byte[leadPadStatusLen];
            System.arraycopy(pData, 21 * 2 + 1 + leadPadStatusLen, leadPadStatus, 0, leadPadStatusLen);
            if (BaseApp.isDebug)
                Log.i(TAG, "导联状态 = " + StringUtil.byte2HexString(leadPadStatus));
            //电量信息
            int voltage = pData[pData.length - 1] & 0xff;
            if (BaseApp.isDebug)
                Log.i(TAG, "voltage = " + voltage);
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BATTERY, voltage, userId));
        } else if (code == 110) {//状态信息
            byte[] bPowerOn = new byte[6];
            System.arraycopy(data, 4, bPowerOn, 0, 6);
            if (BaseApp.isDebug)
                Log.i(TAG, "开机时间 = " + StringUtil.byte2HexString(bPowerOn));

            byte[] bPowerOff = new byte[6];
            System.arraycopy(data, 10, bPowerOff, 0, 6);
            if (BaseApp.isDebug)
                Log.i(TAG, "关机时间 = " + StringUtil.byte2HexString(bPowerOff));

            byte[] bStorage = new byte[4];
            System.arraycopy(data, 16, bStorage, 0, 4);
            int storage = bStorage[0] & 0xff | (bStorage[1] << 8) & 0xff00 | (bStorage[2] << 16) & 0xff0000 | (bStorage[3] << 24) & 0xff000000;
            if (BaseApp.isDebug)
                Log.i(TAG, "剩余存储空间 = " + storage + " M");

            byte[] bSim = new byte[1];
            System.arraycopy(data, 20, bSim, 0, 1);
            if (BaseApp.isDebug)
                Log.i(TAG, "SIM卡状态 = " + StringUtil.byte2HexString(bSim));

            byte[] systemStatus = new byte[4];
            System.arraycopy(data, 21, systemStatus, 0, 4);
            if (BaseApp.isDebug)
                Log.i(TAG, "系统状态 = " + StringUtil.byte2HexString(systemStatus));

            byte[] mqttStatus = new byte[1];
            System.arraycopy(data, 25, mqttStatus, 0, 1);
            if (BaseApp.isDebug)
                Log.i(TAG, "mqtt 服务状态 = " + StringUtil.byte2HexString(mqttStatus));

            byte[] whichServer = new byte[1];
            System.arraycopy(data, 26, whichServer, 0, 1);
            if (BaseApp.isDebug)
                Log.i(TAG, "连接哪个服务器 = " + StringUtil.byte2HexString(whichServer));

            byte[] reconnectCnt = new byte[1];
            System.arraycopy(data, 27, reconnectCnt, 0, 1);
            if (BaseApp.isDebug)
                Log.i(TAG, "重连次数 = " + StringUtil.byte2HexString(reconnectCnt));

            byte[] network = new byte[4];
            System.arraycopy(data, 28, network, 0, 4);
            if (BaseApp.isDebug)
                Log.i(TAG, "网络状态 = " + StringUtil.byte2HexString(network));
            //经纬度
            byte[] latitude = new byte[12];
            System.arraycopy(data, 32, latitude, 0, 12);
            byte[] longitude = new byte[12];
            System.arraycopy(data, 44, longitude, 0, 12);

            LatLngEvent latlng = new LatLngEvent();
            latlng.lat = new String(latitude).trim();
            latlng.lng = new String(longitude).trim();
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_LATITUDE_LONGITUDE, latlng, userId));
            if (BaseApp.isDebug)
                Log.i(TAG, "经纬度 = " + latlng.lat + "," + latlng.lng);

        } else if (code == 112) {//二氧化碳温湿度
            int value1 = data[0] & 0xff | ((data[1] & 0xff) << 8);
            int value2 = data[2] & 0xff | ((data[3] & 0xff) << 8);
            int value3 = data[4] & 0xff | ((data[5] & 0xff) << 8);

            CarbonDioxideEvent dioxideEvent = new CarbonDioxideEvent();

            if (value1 != 0xffff)
                dioxideEvent.value = value1;
            if (value2 != 0x8000)
                dioxideEvent.temp = value2;
            if (value3 != 0xffff)
                dioxideEvent.humidity = value3;

            System.out.println(dioxideEvent.toString());
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE_HUMIDITY, dioxideEvent, userId));
        } else if (code == 113) {//压强
            AtmosphericPressure ap = new AtmosphericPressure();
            if (data.length == 2) {
                ap.value = data[0] & 0xff | ((data[1] & 0xff) << 8);
            } else if (data.length == 4) {
                ap.value = data[0] & 0xff | ((data[1] & 0xff) << 8);
                ap.temp = data[2] & 0xff | ((data[3] & 0xff) << 8);
            } else if (data.length == 6) {
                ap.value = data[0] & 0xff | ((data[1] & 0xff) << 8);
                ap.temp = data[2] & 0xff | ((data[3] & 0xff) << 8);
                ap.humidity = data[4] & 0xff | ((data[5] & 0xff) << 8);
            }
            System.out.println(ap.toString());
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_ATMOSPHERIC_PRESSURE, ap, userId));
        }

    }

    /**
     * 拆分 处理波形数据 使其看着有连续的波形数据
     * 此解析的是多参波形数据 注意顺序
     *
     * @param sData
     */
    private void handWaveData(short[] sData) {

        //resp,I, II，V1, V3, V4, V5, V6
        //III = II-I    aVR =  -0.5*(I+II)   aVL = I - 0.5*II   aVF = II - 0.5*I

        count++;

        short leadI = 0;
        short leadII = 0;

        if (hasRespWave) {
            //解析八个导联的数据
            for (int i = 0; i < sData.length; i++) {
                short data = sData[i];
                if (i % 8 == 0) {
                    listResp.add(data);
                } else if (i % 8 == 1) {
                    leadI = data;
                    listLeadI.add(leadI);
                } else if (i % 8 == 2) {
                    leadII = data;
                    listLeadII.add(leadII);
                    short leadIII = (short) (leadII - leadI);
                    short leadAVR = (short) -((leadI + leadII) / 2);
                    short leadAVL = (short) (leadI - leadII / 2);
                    short leadAVF = (short) (leadII - leadI / 2);
                    listLeadIII.add(leadIII);
                    listLeadAvr.add(leadAVR);
                    listLeadAvl.add(leadAVL);
                    listLeadAvf.add(leadAVF);
                } else if (i % 8 == 3) {
                    listLeadV1.add(data);
                    short leadV2 = (short) (data * 1.544 - 0.28);
                    listLeadV2.add(leadV2);
                } else if (i % 8 == 4) {
                    listLeadV3.add(data);
                } else if (i % 8 == 5) {
                    listLeadV4.add(data);
                } else if (i % 8 == 6) {
                    listLeadV5.add(data);
                } else if (i % 8 == 7) {
                    listLeadV6.add(data);
                }
            }
        } else {
            for (int i = 0; i < sData.length; i++) {
                short data = sData[i];
                if (i % 8 == 0) {
                    leadI = data;
                    listLeadI.add(leadI);
                } else if (i % 8 == 1) {
                    leadII = data;
                    listLeadII.add(leadII);
                    short leadIII = (short) (leadII - leadI);
                    short leadAVR = (short) -((leadI + leadII) / 2);
                    short leadAVL = (short) (leadI - leadII / 2);
                    short leadAVF = (short) (leadII - leadI / 2);
                    listLeadIII.add(leadIII);
                    listLeadAvr.add(leadAVR);
                    listLeadAvl.add(leadAVL);
                    listLeadAvf.add(leadAVF);
                } else if (i % 8 == 2) {
                    listLeadV1.add(data);
                } else if (i % 8 == 3) {
                    listLeadV2.add(data);
                } else if (i % 8 == 4) {
                    listLeadV3.add(data);
                } else if (i % 8 == 5) {
                    listLeadV4.add(data);
                } else if (i % 8 == 6) {
                    listLeadV5.add(data);
                } else if (i % 8 == 7) {
                    listLeadV6.add(data);
                }
            }
        }


        if (("1.0".equals(version) && count == 2) || "1.1".equals(version)) {
            //如果是呼吸率数据则存储起来 用来做计算
            if (hasRespWave && userId.equals(BaseApp.getInstance().getRespId())) {
                int size = BaseApp.getInstance().respData.size();
                if (size >= 7500) {
                    for (int i = 0; i < 1500; i++) {
                        BaseApp.getInstance().respData.removeFirst();
                    }
                }
                BaseApp.getInstance().respData.addAll(listResp);
            }

            List<Short> listI = new ArrayList<>();
            List<Short> listII = new ArrayList<>();
            List<Short> listIII = new ArrayList<>();
            List<Short> listAvr = new ArrayList<>();
            List<Short> listAvl = new ArrayList<>();
            List<Short> listAvf = new ArrayList<>();
            List<Short> listV1 = new ArrayList<>();
            List<Short> listV2 = new ArrayList<>();
            List<Short> listV3 = new ArrayList<>();
            List<Short> listV4 = new ArrayList<>();
            List<Short> listV5 = new ArrayList<>();
            List<Short> listV6 = new ArrayList<>();

            for (int i = 0; i < listLeadII.size(); i++) {
                try {
                    listI.add(listLeadI.get(i));
                    listII.add(listLeadII.get(i));
                    listIII.add(listLeadIII.get(i));
                    listAvr.add(listLeadAvr.get(i));
                    listAvl.add(listLeadAvl.get(i));
                    listAvf.add(listLeadAvf.get(i));
                    listV1.add(listLeadV1.get(i));
                    listV2.add(listLeadV2.get(i));
                    listV3.add(listLeadV3.get(i));
                    listV4.add(listLeadV4.get(i));
                    listV5.add(listLeadV5.get(i));
                    listV6.add(listLeadV6.get(i));

                    if ((i + 1) % 10 == 0) {
                        List<Short> subI = new ArrayList(listI);
                        List<Short> subII = new ArrayList(listII);
                        List<Short> subIII = new ArrayList(listIII);
                        List<Short> subAvr = new ArrayList(listAvr);
                        List<Short> subAvl = new ArrayList(listAvl);
                        List<Short> subAvf = new ArrayList(listAvf);
                        List<Short> subV1 = new ArrayList(listV1);
                        List<Short> subV2 = new ArrayList(listV2);
                        List<Short> subV3 = new ArrayList(listV3);
                        List<Short> subV4 = new ArrayList(listV4);
                        List<Short> subV5 = new ArrayList(listV5);
                        List<Short> subV6 = new ArrayList(listV6);
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_I, subI, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_II, subII, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_III, subIII, userId));

                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVR, subAvr, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVL, subAvl, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVF, subAvf, userId));

                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V1, subV1, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V2, subV2, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V3, subV3, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V4, subV4, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V5, subV5, userId));
                        EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V6, subV6, userId));
                        listI.clear();
                        listII.clear();
                        listIII.clear();
                        listAvr.clear();
                        listAvl.clear();
                        listAvf.clear();
                        listV1.clear();
                        listV2.clear();
                        listV3.clear();
                        listV4.clear();
                        listV5.clear();
                        listV6.clear();
                        Thread.sleep(39);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
